package org.absolutezero.model;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Version;



@Entity
@Table(name="fridge_product")
public class FridgeProduct {

	 private static Logger logger = Logger.getLogger(User.class.getName());
	
	 @Id
	 @GeneratedValue(strategy = GenerationType.IDENTITY)
	 private Long id;
	 
	 @Version
	 private Integer version;
	 
	 private Long quantity;
	 
	 private Date expiration;
	 
	 @ManyToOne(fetch=FetchType.EAGER)
	 @JoinColumn(name="self_id")
	 private Shelf shelf;
	 
	 @ManyToOne(fetch=FetchType.EAGER)
	 @JoinColumn(name="product_id")
	 private Product product;

	public Shelf getShelf() {
		return shelf;
	}

	public void setShelf(Shelf shelf) {
		this.shelf = shelf;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Long getQuantity() {
		return quantity;
	}

	public void setQuantity(Long quantity) {
		this.quantity = quantity;
	}

	public Date getExpiration() {
		return expiration;
	}

	public void setExpiration(Date expiration) {
		this.expiration = expiration;
	}
	
	
	public void persist() {
    	
	    EntityManager em = entityManager();
	    
	    try {
	    	
	      EntityTransaction tx  = em.getTransaction();
 	      tx.begin();
 	      this.product = em.merge(this.product);
 	      this.shelf = em.merge(this.shelf);
 	      em.persist(this);
	      tx.commit();
	     
	    } finally {
	      em.close();
	    }
	  }

	public void remove() {
	    EntityManager em = entityManager();
	    try {
	
	      EntityTransaction tx  = em.getTransaction();
	      FridgeProduct p = em.find(FridgeProduct.class, this.id);
	      em.remove(p);
	      tx.commit();
	    } finally {
	      em.close();
	    }
	  }
	
	public static FridgeProduct findFridgeProduct(Long id){
 		if (id == null) {
 		      return null;
	    }
	    EntityManager em = entityManager();
	    try {
	      FridgeProduct fridgeProduct = em.find(FridgeProduct.class, id);
	      return fridgeProduct;
	    } finally {
	      em.close();
	    }
 	}
	
	public static List<FridgeProduct> getShelfProducts(Long shelfId){
		EntityManager em = entityManager();
		Query q = em.createQuery("SELECT fp FROM FridgeProduct fp WHERE fp.shelf.id=:id");
		q.setParameter("id", shelfId);
		
		return (List<FridgeProduct>)q.getResultList();
	}
	
	 private static EntityManager entityManager(){
	     return EMF.get().createEntityManager();
	 }
	
}
